<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="订单ID" prop="orderId">
        <el-input v-model="formData.orderId" placeholder="请输入订单ID" />
      </el-form-item>
      <el-form-item label="下单会员ID" prop="memberId">
        <el-input v-model="formData.memberId" placeholder="请输入下单会员ID" />
      </el-form-item>
      <el-form-item label="店铺ID" prop="teamId">
        <el-select v-model="formData.teamId" placeholder="请选择店铺ID">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="操作员工ID" prop="teamStaffId">
        <el-select v-model="formData.teamStaffId" placeholder="请选择操作员工ID">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="商品ID" prop="productId">
        <el-input v-model="formData.productId" placeholder="请输入商品ID" />
      </el-form-item>
      <el-form-item label="商品编码" prop="productCode">
        <el-input v-model="formData.productCode" placeholder="请输入商品编码" />
      </el-form-item>
      <el-form-item label="成本价" prop="costPrice">
        <el-input v-model="formData.costPrice" placeholder="请输入成本价" />
      </el-form-item>
      <el-form-item label="销售价" prop="salePrice">
        <el-input v-model="formData.salePrice" placeholder="请输入销售价" />
      </el-form-item>
      <el-form-item label="退款数量" prop="refundNum">
        <el-input v-model="formData.refundNum" placeholder="请输入退款数量" />
      </el-form-item>
      <el-form-item label="退款金额" prop="refundMoney">
        <el-input v-model="formData.refundMoney" placeholder="请输入退款金额" />
      </el-form-item>
      <el-form-item label="退款时间" prop="refundTime">
        <el-date-picker
          v-model="formData.refundTime"
          type="date"
          value-format="x"
          placeholder="选择退款时间"
        />
      </el-form-item>
      <el-form-item label="退款原因" prop="refundReason">
        <el-input v-model="formData.refundReason" placeholder="请输入退款原因" />
      </el-form-item>
      <el-form-item label="优惠金额" prop="discountMoney">
        <el-input v-model="formData.discountMoney" placeholder="请输入优惠金额" />
      </el-form-item>
      <el-form-item label="身份特权优惠" prop="identityDiscountMoney">
        <el-input v-model="formData.identityDiscountMoney" placeholder="请输入身份特权优惠" />
      </el-form-item>
      <el-form-item label="接龙优惠" prop="dragonDiscountMoney">
        <el-input v-model="formData.dragonDiscountMoney" placeholder="请输入接龙优惠" />
      </el-form-item>
      <el-form-item label="是否特价" prop="isSpecial">
        <el-radio-group v-model="formData.isSpecial">
          <el-radio
            v-for="dict in getIntDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING)"
            :key="dict.value"
            :label="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="商品购买数量" prop="productNum">
        <el-input v-model="formData.productNum" placeholder="请输入商品购买数量" />
      </el-form-item>
      <el-form-item label="实付金额" prop="productMoney">
        <el-input v-model="formData.productMoney" placeholder="请输入实付金额" />
      </el-form-item>
      <el-form-item label="退款状态" prop="refundStatus">
        <el-select v-model="formData.refundStatus" placeholder="请选择退款状态">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="退款类型" prop="refundType">
        <el-select v-model="formData.refundType" placeholder="请选择退款类型">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="formData.status">
          <el-radio
            v-for="dict in getIntDictOptions(DICT_TYPE.COMMON_STATUS)"
            :key="dict.value"
            :label="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { SaleRecordApi, SaleRecordVO } from '@/api/team/teamsalerecord'

/** 店铺销售记录 表单 */
defineOptions({ name: 'SaleRecordForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  orderId: undefined,
  memberId: undefined,
  teamId: undefined,
  teamStaffId: undefined,
  productId: undefined,
  productCode: undefined,
  costPrice: undefined,
  salePrice: undefined,
  refundNum: undefined,
  refundMoney: undefined,
  refundTime: undefined,
  refundReason: undefined,
  discountMoney: undefined,
  identityDiscountMoney: undefined,
  dragonDiscountMoney: undefined,
  isSpecial: undefined,
  id: undefined,
  productNum: undefined,
  productMoney: undefined,
  refundStatus: undefined,
  refundType: undefined,
  status: undefined,
})
const formRules = reactive({
  status: [{ required: true, message: '状态不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await SaleRecordApi.getSaleRecord(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as SaleRecordVO
    if (formType.value === 'create') {
      await SaleRecordApi.createSaleRecord(data)
      message.success(t('common.createSuccess'))
    } else {
      await SaleRecordApi.updateSaleRecord(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    orderId: undefined,
    memberId: undefined,
    teamId: undefined,
    teamStaffId: undefined,
    productId: undefined,
    productCode: undefined,
    costPrice: undefined,
    salePrice: undefined,
    refundNum: undefined,
    refundMoney: undefined,
    refundTime: undefined,
    refundReason: undefined,
    discountMoney: undefined,
    identityDiscountMoney: undefined,
    dragonDiscountMoney: undefined,
    isSpecial: undefined,
    id: undefined,
    productNum: undefined,
    productMoney: undefined,
    refundStatus: undefined,
    refundType: undefined,
    status: undefined,
  }
  formRef.value?.resetFields()
}
</script>